package com.wocai.jrt.orgadmin.stats.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wocai.jrt.orgadmin.employee.model.Org;
import com.wocai.jrt.orgadmin.employee.service.spi.OrgService;
import com.wocai.jrt.orgadmin.stats.bean.OrgProductEmpStatsResBean;
import com.wocai.jrt.orgadmin.stats.bean.ProductEmpStatsReqBean;
import com.wocai.jrt.orgadmin.stats.bean.ProductRecruitProgressResBean;
import com.wocai.jrt.orgadmin.stats.bean.ProductStatResBean;
import com.wocai.jrt.orgadmin.stats.service.spi.ProductStatsJobService;
import com.wocai.jrt.orgadmin.stats.service.spi.ProductStatsService;

@Named
public class ProductStatsJobServiceImpl implements ProductStatsJobService{
	private static final Logger LOGGER = LoggerFactory.getLogger(ProductStatsJobServiceImpl.class);
	@Inject
	private OrgService orgServiceImpl;
	@Inject
	private ProductStatsService productStatsServiceImpl;
	
	/**
	 * 定时任务:将每个机构的产品统计数据存入redis
	 * 每一天执行一次
	 */
	public void productStatsTask(){
		try {
			List<Org> orgList = orgServiceImpl.queryList(null);
			for(Org org:orgList){
				@SuppressWarnings("unused")
				List<ProductStatResBean> productStatsList = new ArrayList<ProductStatResBean>();
				@SuppressWarnings("unused")
				ProductRecruitProgressResBean prp = new ProductRecruitProgressResBean();
				@SuppressWarnings("unused")
				OrgProductEmpStatsResBean eBean = new OrgProductEmpStatsResBean();
				
				ProductEmpStatsReqBean productEmpStatsReqBean = new ProductEmpStatsReqBean();
				productEmpStatsReqBean.setOrgId(org.getId());
				
				try {
					productStatsList = productStatsServiceImpl.putProductStatsResBeanListToRedis(org.getId());
					prp = productStatsServiceImpl.putUncompletedProductRecruitProgressToRedis(org.getId());
					eBean = productStatsServiceImpl.putOrgProductEmpStatsResBeanToRedis(productEmpStatsReqBean);
					productStatsList = null;
					prp = null;
					eBean = null;
					System.gc();
				} catch (Exception e) {
					LOGGER.error("productStatsTask running error .msg=[{}]", e.getMessage());
					e.printStackTrace();
					continue;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("Timed Task error, error=[{}]", e.getMessage());
		}
	}
}
